package com.ellwood.copyright.framework.config.druid;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.jakarta.StatViewServlet;
import com.alibaba.druid.support.jakarta.WebStatFilter;
import lombok.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @ProjectName: ell-copyright-pro
 * @PackageName: com.ellwood.copyright.framework.config.druid
 * @ClassName: DruidConfig
 * @Description: // DruidConfig 配置以及可视化监控配置
 * @Author: Ellwood
 * @CreateDate: 2024/11/21 - 14:47
 * @Version: V1.0.0
 */
@Configuration
public class DruidConfig {

    /**
     * 配置Druid监控表的servlet
     * @return ServletRegistrationBean<StatViewServlet>
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> druidStatViewServlet(){
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        // 白名单 (允许访问的IP)
        servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); // 仅允许本地访问

        // 黑名单 (拒绝访问的IP)
        servletRegistrationBean.addInitParameter("deny", "192.168.1.100"); // 拒绝此IP访问

        // 登录的账号和密码
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "admin");

        // 是否可以重置数据
        servletRegistrationBean.addInitParameter("resetEnable", "false");

        return servletRegistrationBean;
    }

    /**
     * 配置Druid监控的过滤器
     * @return FilterRegistrationBean<WebStatFilter>
     */
    @Bean
    public FilterRegistrationBean<WebStatFilter> druidWebStatFilter(){
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(new WebStatFilter());
        // 添加过滤规则
        filterRegistrationBean.addUrlPatterns("/*");
        // 添加忽略规则,不拦截的静态资源
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");

        return filterRegistrationBean;
    }

}
